/*
 ============================================================================
 Name		: ResponseSort.cpp
 Author	  : 
 Version	 : 1.0
 Copyright   :
 Description : CResponseSort implementation
 ============================================================================
 */

#include "ResponseSort.h"
#include "ResponseDefinses.h"
#include "ProjectsDefines.h"
#include "ResponseSortData.h"
#include "ResponseMagazineItemData.h"


CResponseSort::CResponseSort()
{
    // No implementation required
}

CResponseSort::~CResponseSort()
{
    SAFE_DELETE( _data );
}

CResponseSort* CResponseSort::NewLC()
{
    CResponseSort* self = new ( ELeave ) CResponseSort( );
    CleanupStack::PushL( self );
    self->ConstructL( );
    return self;
}

CResponseSort* CResponseSort::NewL()
{
    CResponseSort* self = CResponseSort::NewLC( );
    CleanupStack::Pop( ); // self;
    return self;
}

void CResponseSort::ConstructL()
{
    _data = CResponseSortData::NewL( );
}

void CResponseSort::LoadMagazineItemsNode(CElement* element, RPointerArray< CResponseMagazineItemData >* container)
{
    int count = element->ElementCount( );
    for( int index = 0; index < count; index++ )
    {
        CElement* elementItem = element->GetElement( index );
        CResponseMagazineItemData* magazineItem = ResponseMagazineItemDataParser::Parse( elementItem );
        if( magazineItem )
        {
            container->Append( magazineItem );
        }
    }
}

void CResponseSort::LoadValidResponseFromXmlNode(CElement* elementResponse)
{
    _ASSERT_LOG( elementResponse != NULL );

    if( !elementResponse )
    {
        return;
    }
    _LIT( kResponseMonthTopNode, "month_top" );
    _LIT( kResponseWeekTopNode, "week_top" );
    _LIT( kResponseGoodTopNode, "good_top" );
    _LIT( kResponseBadTopNode, "bad_top" );

    CElement* element = elementResponse->GetElement( kResponseMonthTopNode );
    if( element )
    {
        LoadMagazineItemsNode( element, &_data->_month );
    }

    element = elementResponse->GetElement( kResponseWeekTopNode );
    if( element )
    {
        LoadMagazineItemsNode( element, &_data->_week );
    }

    element = elementResponse->GetElement( kResponseGoodTopNode );
    if( element )
    {
        LoadMagazineItemsNode( element, &_data->_good );
    }

    element = elementResponse->GetElement( kResponseBadTopNode );
    if( element )
    {
        LoadMagazineItemsNode( element, &_data->_bad );
    }
}
